package com.dmyang.common.pojo;



import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

/**
 * 本网站统一响应pojo。（如果不知道向前台返回什么，就返回它）
 * Created by brss on 2017/8/3.
 */
public class HyResult {

    // 响应业务状态
    private Integer status;

    // 响应消息
    private String msg;

    // 响应中的数据
    private Object data;

    public static HyResult build(Integer status, String msg, Object data) {
        return new HyResult(status, msg, data);
    }

    public static HyResult ok(Object data) {
        return new HyResult(data);
    }

    public static HyResult ok() {
        return new HyResult(null);
    }

    public HyResult() {

    }

    public static HyResult build(Integer status, String msg) {
        return new HyResult(status, msg, null);
    }

    public HyResult(Integer status, String msg, Object data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public HyResult(Object data) {
        this.status = 200;
        this.msg = "OK";
        this.data = data;
    }

//    public Boolean isOK() {
//        return this.status == 200;
//    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    /**
     * 将json结果集转化为TaotaoResult对象
     *
     * @param jsonData json数据
     * @param clazz TaotaoResult中的object类型
     * @return
     */
    public static HyResult formatToPojo(String jsonData, Class<?> clazz) {
        try {
            if (clazz == null) {
                //clazz为空就直接转换成本对象
                return JSON.parseObject(jsonData, HyResult.class);
            }
            JSONObject jsonObject = JSON.parseObject(jsonData);

            Object obj = null;
            if (clazz != null) {
                obj = jsonObject.getObject("data",clazz);
            }
            return build((Integer)jsonObject.get("status"), (String) jsonObject.get("msg") , obj);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 没有object对象的转化
     *
     * @param json
     * @return
     */
    public static HyResult format(String json) {
        try {
            return JSON.parseObject(json, HyResult.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Object是集合转化
     *
     * @param jsonData json数据
     * @param clazz    集合中的类型
     * @return
     */
    public static HyResult formatToList(String jsonData, Class<?> clazz) {
        try {
            JSONObject jsonObject = JSON.parseObject(jsonData);
            JSONArray data = jsonObject.getJSONArray("data");
            List<?> obj= JSON.parseArray(data.toJSONString(), clazz);
            return build((Integer) jsonObject.get("status"), (String) jsonObject.get("msg"), obj);
        } catch (Exception e) {
            return null;
        }
    }
}
